var chapterList;
var cid;
let title = document.getElementById("title");
let text = document.getElementById("text");
let params = new URLSearchParams();
params.append('cid', request("cid"))
axios.post("getChapterContent",params)
    .then(function (response) {
        let chapter = response.data.chapter;
        title.innerText = chapter.name;
        text.innerText = chapter.content;
    });

function mode() {
    let mode = document.getElementsByClassName("mode");
    if(mode[0].className.includes("sun")){
        //转换为夜间模式
        for(let index=0;index<mode.length;index++){
            document.body.style.backgroundColor = "rgba(0,0,0,0.87)";
            document.getElementById("content").style.color="#FFF";
            mode[index].classList.remove("sun");
            mode[index].classList.add("moon");
        }
    }else {
        //转换为日间模式
        for(let index=0;index<mode.length;index++){
            document.body.style.backgroundColor="#F4F4F4";
            document.getElementById("content").style.color="#000";
            mode[index].classList.remove("moon");
            mode[index].classList.add("sun");
        }
    }
}
//上一章
function previous(){
    let index;
    console.log(chapterList);
    for(index=0;index<chapterList.length;index++){
        console.log(index+":"+cid+":"+chapterList[index].cid);
        //chapterList中cid为int ，cid为String 直接用==比较
        if(chapterList[index].cid==cid){
            break;
        }
    }
    let params = new URLSearchParams();
    params.append('cid', chapterList[index-1].cid);
    //内存中的cid更换
    cid=chapterList[index-1].cid;
    axios.post("getChapterContent",params)
        .then(function (response) {
            let chapter = response.data.chapter;
            title.innerText = chapter.name;
            text.innerText = chapter.content;
        });
}
//下一章
function next() {
    let index;
    console.log(chapterList);
    for(index=0;index<chapterList.length;index++){
        console.log(index+":"+cid+":"+chapterList[index].cid);
        //chapterList中cid为int ，cid为String 直接用==比较
        if(chapterList[index].cid==cid){
            break;
        }
    }
    let params = new URLSearchParams();
    params.append('cid', chapterList[index+1].cid);
    //内存中的cid更换
    cid=chapterList[index+1].cid;
    axios.post("getChapterContent",params)
        .then(function (response) {
            let chapter = response.data.chapter;
            title.innerText = chapter.name;
            text.innerText = chapter.content;
        });
}
//返回书籍页面
function back(){
    window.location="book.html?bid="+request("bid")+"&name="+request("name")
        +"&brief="+request("brief")+"&cover="+request("cover");
}

window.onload =function (){
    //初始化章节列表
    cid =request("cid");
    document.getElementById("book-name").innerText =request("name");
    axios.get("/getChapterNameList?bid="+request("bid"))
        .then(function (response) {
            chapterList =response.data.chapterList;
        });


}
